package com.xcoder.leetcode;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/score-of-parentheses/
 */
public class _856_括号的分数_中等 {

    /**
     * 栈
     */
    public static int scoreOfParentheses(String S) {
        Stack<Integer> stack = new Stack<>();
        int cur = 0;
        for (int i = 0, size = S.length(); i < size; i++) {
            char c = S.charAt(i);
            if (c == '(') {
                stack.push(cur);
                cur = 0;
            } else {
                cur += stack.pop() + Math.max(cur, 1);
            }
        }
        return cur;
    }

    public static void main(String[] args) {
        String s = "(()(()))";
        System.out.println(scoreOfParentheses(s));
    }
}
